package cn.lzit.session;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        out.print("<p>这里是结账处, 您的姓名以及选择的商品是：</p>");

        HttpSession session = request.getSession();
        String personName = (String) session.getAttribute("customerName");
        out.println("<br>您的姓名：" + personName);

        Enumeration<String> enumGoods = session.getAttributeNames();
        out.println("<br>购物车中的商品：<br>");

        while (enumGoods.hasMoreElements()) {
            String key = enumGoods.nextElement();
            String goods = (String) session.getAttribute(key);
            if (!goods.equals(personName)) {
                out.println(goods + "<br>");
            }
        }

        out.println("<p>点击超链接，链接到food.html的页面，购买食品。</p>");
        out.println("<a href='food.html'>欢迎去food.html！</a>");
        out.println("<p>点击超链接，链接到login.html的页面, 去修改姓名。</p>");
        out.println("<a href='login.html'>欢迎去login.html！</a>");
        out.println("<p>点击超链接，链接到logout页面, 退出</p>");
        out.println("<a href='LogOutServlet'>退出！</a>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
